package com.maxjay.system.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.maxjay.common.util.collection.CollectionUtil;
import com.maxjay.system.model.DictCode;
import com.maxjay.system.model.DictTab;
import com.maxjay.system.service.BaseServiceJdbc;
import com.maxjay.system.service.DictTableService;

@Service("dictTableService")
public class DictTableServiceImpl extends BaseServiceJdbc implements DictTableService {
	
	private static final Logger LOG = Logger.getLogger(DictTableServiceImpl.class);
	
	public DictTab loadDictTab(String dictTabId) {
		String qryDictTabSql = "select * from T_DICT_TAB t where t.ID = ?";
		LOG.debug("查询表码表[" + dictTabId + "]的SQL为：" + qryDictTabSql);
		DictTab dictTab = this.getSimpleJdbcTemplate().queryForObject(qryDictTabSql, new RowMapper<DictTab>() {
			@Override
			public DictTab mapRow(ResultSet rs, int index) throws SQLException {
				DictTab dictTab = new DictTab();
				dictTab.setId(rs.getString(DictTab.ID_FIELD));
				dictTab.setTabCode(rs.getString(DictTab.TAB_CODE_FIELD));
				dictTab.setTabName(rs.getString(DictTab.TAB_NAME_FIELD));
				dictTab.setTabType(rs.getString(DictTab.TAB_TYPE_FIELD));
				dictTab.setTabDesc(rs.getString(DictTab.TAB_DESC_FIELD));
				dictTab.setCodeField(rs.getString(DictTab.CODE_FIELD_FIELD));
				dictTab.setNameField(rs.getString(DictTab.NAME_FIELD_FIELD));
				return dictTab;
			}
		}, dictTabId);
		loadDictCodes(dictTab);
		return dictTab;
	}
	
	private void loadDictCodes(DictTab dictTab) {
		List<DictCode> dictCodes = null;
		if (DictTab.NORMAL_DICT_TAB.equals(dictTab.getTabType())) {
			String qryDictCodeSql = "select * from T_DICT_CODE t where t.DICT_ID = ?";
			LOG.debug("查询正规字典表[" + dictTab.getId() + "]表码值的SQL为：" + qryDictCodeSql);
			dictCodes = this.getSimpleJdbcTemplate().query(qryDictCodeSql, new RowMapper<DictCode>() {
				@Override
				public DictCode mapRow(ResultSet rs, int index) throws SQLException {
					DictCode dictCode = new DictCode(rs.getString(DictCode.CODE_FIELD), rs.getString(DictCode.NAME_FIELD));
					return dictCode;
				}
			}, dictTab.getId());
		} else if (DictTab.ISOMERISM_DICT_TAB.equals(dictTab.getTabType())) {
			final String codeField = dictTab.getCodeField();
			final String nameField = dictTab.getCodeField();
			String qryDictCodeSql = "select t." + dictTab.getCodeField() + ", t." + dictTab.getNameField() + 
					" from " + dictTab.getTabCode() + " t";
			LOG.debug("查询异构字典表[" + dictTab.getId() + "]表码值的SQL为：" + qryDictCodeSql);
			dictCodes = this.getSimpleJdbcTemplate().query(qryDictCodeSql, new RowMapper<DictCode>() {
				@Override
				public DictCode mapRow(ResultSet rs, int index) throws SQLException {
					DictCode dictCode = new DictCode(rs.getString(codeField), rs.getString(nameField));
					return dictCode;
				}
			}, dictTab.getId());
		}
		if (CollectionUtil.isNotEmpty(dictCodes)) {
			dictTab.setDictCodes(dictCodes);
		}
		LOG.info("加载表码表[" + dictTab.getId() + "]，共" + dictTab.getDictCodes().size() + "行记录");
	}
	
}